import 'package:byw_blog/database/DatabaseHelp.dart';
import 'package:byw_blog/pages/HomePage.dart';
import 'package:byw_blog/pages/MyPage.dart';
import 'package:byw_blog/pages/RealTimeInfoPage.dart';
import 'package:byw_blog/utils/AppUtil.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const BYWBlog());
}

class BYWBlog extends StatefulWidget {
  const BYWBlog({super.key});

  @override
  State<StatefulWidget> createState() => _BYWBlogState();
}

class _BYWBlogState extends State<BYWBlog> {
  final appBarTitles = ['首页', '发现', '我的'];
  var pages, tabImages;
  var _body;
  int _tabIndex = 0;

  @override
  void initState() {
    super.initState();
    pages = <Widget>[const HomePage(), const RealTimeInfoPage(),const MyPage()];
    tabImages = [
      [
        AppUtil.getImage20('images/ic_nav_news_normal.png'),
        AppUtil.getImage20('images/ic_nav_news_actived.png')
      ],
      [
        AppUtil.getImage20('images/ic_nav_discover_normal.png'),
        AppUtil.getImage20('images/ic_nav_discover_actived.png')
      ],
      [
        AppUtil.getImage20('images/ic_nav_my_normal.png'),
        AppUtil.getImage20('images/ic_nav_my_pressed.png')
      ]
    ];

    DatabaseHelp.initDb();
  }

  @override
  Widget build(BuildContext context) {
    _body = IndexedStack(
      children: pages,
      index: _tabIndex,
    );
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primaryColor: Colors.teal),
      home: Scaffold(
        body: _body,
        bottomNavigationBar: CupertinoTabBar(
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(icon: getTabIcon(0), label: getTabTitle(0)),
            BottomNavigationBarItem(icon: getTabIcon(1), label: getTabTitle(1)),
            BottomNavigationBarItem(icon: getTabIcon(2), label: getTabTitle(2))
          ],
          currentIndex: _tabIndex,
          onTap: (index) {
            setState(() {
              _tabIndex = index;
            });
          },
        ),
      ),
    );
  }

  String getTabTitle(int curIndex) {
    return appBarTitles[curIndex];
  }

  Image getTabIcon(int curIndex) {
    if (curIndex == _tabIndex) {
      return tabImages[curIndex][1];
    }
    return tabImages[curIndex][0];
  }
}
